<?php

include_once('dao_package.php');
include_once('dao_common.php');
include_once('dao_tables.php');
include_once('../../config/dao.cfg.php');
include_once('dao_mcache.php');
include_once('dao_bo_session.php');

//

class DAO_Bo
{
	public $need_resp = false;
	public $need_req_data = 0;
	//
	public $convs_field =array();
	public $html_field=array();
	//in-->
	public $convs_in=array();
	//out<--
	public $convs_out=array();
	/*定义可能包含html文本的field,
	[in]文本以html格式保存到数据库，
	[out]从数据库出来到接口，则需要进行html编码，以免影响xml结构
	*/
	//in-->
	public $html_field_in=array();
	//out<--
	public $html_field_out=array();
	public $type='';

	function __construct()
	{
		$this->init();
	}
	function __destruct()
	{
	}

	function init()
	{
	}

	public function get_type()
	{
		if(strlen($this->type)!=0)return $this->type;
		$type =  get_class($this);
		$prefix = 'b_';
		$l_prefix = strlen($prefix);

		if(strncmp($type,$prefix,$l_prefix)==0
		&&strlen($type)>$l_prefix)
		{
			$type = substr($type,$l_prefix);
		}
		$this->type = $type;
		return $this->type;
	}

	public function set_response(&$req,&$resp,$ret,$msg)
	{
		$resp->set_header('retcode',$ret);
		$resp->set_header('reason', $outmsg);


		if(isset($req))
		{

			$resp->type=$req->getRespType();
			$resp->set_header('charset',$req->get_header('charset'));
			$resp->set_header('style',$req->get_header('style'));
			$resp->set_header('data_encode',$req->header['data_encode']);
			if(strlen($resp->format)==0)
			{
				$resp->format = $req->format;
			}
			if(isset($req->header['dataload']))
			{
				$dataload = $resp->count_data_load();
				$resp->header['datalen']=$dataload['datalen'];
				$resp->header['xmllen']=$dataload['xmllen'];
				$resp->header['load']=$dataload['load'];
				DAO_Log::debug('dataload:'.vexport($dataload,"\t"));
			}
		}

		DAO_Log::debug( 'response format = '.$resp->format);
		$size_of_data =sizeof($resp->data);
		if($size_of_data>0)
		{
			$resp->header['datacount']=$size_of_data;
		}
		$resp->header['cached']=bo_session_get('cached');
		if(strlen($resp->type)==0)
		{
			$resp->type='default';
		}
	}
	
	//------------相当于虚函数，由子类实现------------------------
	public function business(&$req,&$resp,&$msg)
	{
		return 0;
	}
	
	//-------------------------------------------------------
	//检查输入输出参数，进行转码，统一日志
	public function execute(&$req,&$resp,&$msg)
	{

		DAO_Log::debug($req->type.' execute start!');
		$start_tick = snap_time();
		$start_time=time();
		//检查请求
		$msg='';
		if(!isset($req))
		{
			$msg= 'request is empty;';
			$ret=-1;
		}
		//检查回应包，如果存在，要求是DAOPackage
		if(isset($resp))
		{
			if(!is_a($resp,'daopackage'))
			{
				$msg.= 'resp is not DAOPackage;';
				$ret=-1;
			}
		}
		if($this->need_req_data>0&&$req->is_data_empty())
		{
			$msg.= 'request data is empty;';
			$ret=-1;
		}
		if($need_resp&&!issset($resp))
		{
			$msg.= 'response buffer is empty;';
			$ret=-1;
		}

		//----------------------------------------------------
		if($ret == 0)
		{
			DAO_Log::debug('input reqeust data:'.$req->dump(CRLF,false));
			$cache = $req->header['cache'];
			if(strlen($cache)&&is_numeric($cache))
			{
				MCache::$use_cache = $cache;
			}
			$ret  = $this->business($req,$resp,$msg);
			DAO_Log::debug($req->type."----------------->ret=$ret  msg=$msg\r\n");
		}
		$this->set_response($req,$resp,$ret,$msg);

		$stop_tick = snap_time();
		$stop_time = time();
		//写业务日志
		$trace_message = $req->type.",$ret".',';
		//数据量
		$trace_message.=sizeof($resp->data).',';

		if($stop_time-$start_time>=13 || ($req->type=='task_query_req' && $stop_time-$start_time>=3))
		{
			$trace_message.='timeout,';
		}

		//用时(ms)
		$trace_message.=sprintf("%0.1f",($stop_tick - $start_tick).',');
		DAO_Log::info($trace_message,'bo');
		return $ret;
	}
};

?>